package com.hnjme.cnec.mdm.endpoints;

import com.google.gson.Gson;
import com.hnjme.cnec.mdm.model.Organization;
import com.hnjme.cnec.mdm.model.Staff;
import com.hnjme.cnec.mdm.service.MasterDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author outta
 * @Since 1.0 2018/8/24
 */
@Slf4j
@RestController
@Api(value = "主数据接口", tags = "MasterDataEndpoints")
@RequestMapping("")
public class MasterDataEndpoints {

	@Autowired
	private MasterDataService masterDataService;

	/**
	 * 查询全部的组织机构（单位）信息
	 *
	 * @param orgType 机构类型
	 * @param page    当前页数
	 * @param limit   每页大小，limit>0采用分页；limit = 0或者为空表示不分页
	 * @return
	 */
	@GetMapping("organization/units")
	@ApiOperation(value = "查询全部的单位信息", notes = "查询全部的单位信息", response = Organization.class)
	public ResponseEntity organizations(@ApiParam(value = "查询视图类型", allowEmptyValue = true, allowableValues = "01,02", example = "01:行政组织机构；02:财务组织机构；为空时查询全部")
										@RequestParam(value = "orgType", required = false) String orgType,
										@ApiParam(value = "数据更新时间，格式为：yyyy-MM-dd HH:mm:ss。该参数表示查询的数据记录更新时间在给定的时间到当前时刻范围内；若为空，则表示查询全部符合其他条件的记录", allowEmptyValue = true,example = "2018-11-01 00:00:00")
										@RequestParam(value = "updateTime", required = false) String updateTime,
										@ApiParam(value = "当前页数")
										@RequestParam(value = "page", required = false) Integer page,
										@ApiParam(value = "每页大小，若该值为空或者为0，表示不使用分页，查询全部符合条件的记录；若值大于0，则对查询结果进行分页")
										@RequestParam(value = "limit", required = false) Integer limit) {
		log.debug("==> \t进入查询全部的单位信息端点，传入参数：orgType={}", orgType);
		List<Organization> organizations = masterDataService.queryUnits(orgType, updateTime, page, limit);
		log.info("查询到{}条记录", organizations.size());
		ResponseEntity result = new ResponseEntity(organizations, HttpStatus.OK);
		return result;
	}

	@GetMapping("organization/{code}")
	@ApiOperation(value = "查询组织机构基本信息", notes = "查询组织机构基本信息", response = Organization.class)
	public ResponseEntity organization(@ApiParam("单位代码")
									   @PathVariable("code") String code) {
		log.debug("==> \t进入查询组织机构基本信息端点，传入参数：code={}", code);
		Organization organization = masterDataService.findOrganization(code);
		log.info("查询到机构代码为{}的组织机构基本信息:{}", code, new Gson().toJson(organization));
		ResponseEntity result = new ResponseEntity(organization, HttpStatus.OK);
		return result;
	}

	@GetMapping("organization/{unitCode}/departments")
	@ApiOperation(value = "查询单位的部门列表", notes = "查询单位的部门列表", response = Organization.class)
	public ResponseEntity organizations(@ApiParam(value = "单位代码")
										@PathVariable(value = "unitCode") String unitCode,
										@ApiParam(value = "查询视图类型", allowEmptyValue = true, allowableValues = "01,02", example = "01:行政组织机构；02:财物组织机构；为空时查询全部")
										@RequestParam(value = "orgType", required = false) String orgType,
										@ApiParam(value = "数据更新时间，格式为：yyyy-MM-dd HH:mm:ss。该参数表示查询的数据记录更新时间在给定的时间到当前时刻范围内；若为空，则表示查询全部符合其他条件的记录", example = "2018-11-01 00:00:00", allowEmptyValue = true)
											@RequestParam(value = "updateTime", required = false) String updateTime,
										@ApiParam(value = "当前页数", defaultValue = "0")
										@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
										@ApiParam(value = "每页大小，若该值为空或者为0，表示不使用分页，查询全部符合条件的记录；若值大于0，则对查询结果进行分页", defaultValue = "0")
										@RequestParam(value = "limit", required = false, defaultValue = "0") Integer limit) {
		log.debug("==> \t进入查询单位的部门列表端点，传入参数：unitCode={};orgType={}", unitCode, orgType);

		List<Organization> organizations = masterDataService.queryDepartments(unitCode, orgType, updateTime,page, limit);
		log.info("查询到{}条记录", organizations.size());
		ResponseEntity result = new ResponseEntity(organizations, HttpStatus.OK);
		return result;
	}


//    @GetMapping("staffs")
//    @ApiOperation(value = "查询全部员工列表", notes = "使用场景：<br> 服务消费者希望将员工数据落地，并在应用中使用本地数据<br>服务消费者需要定时同步数据，以保持本地数据和服务提供者一致",response = Staff.class)
//    public ResponseEntity staffs() {
//        log.debug("==> \t进入查询全部员工列表端点");
//
//        List<Staff> staffs = masterDataService.queryStaffs();
//        log.info("查询到{}条记录", staffs.size());
//        ResponseEntity result = new ResponseEntity(staffs, HttpStatus.OK);
//        return result;
//    }

	@GetMapping("{unitCode}/staffs")
	@ApiOperation(value = "查询单位的员工列表", notes = "查询单位的员工列表", response = Staff.class)
	public ResponseEntity staffs(@ApiParam(value = "单位代码", required = true)
								 @PathVariable("unitCode") String unitCode,
								 @ApiParam(value = "数据更新时间，格式为：yyyy-MM-dd HH:mm:ss。该参数表示查询的数据记录更新时间在给定的时间到当前时刻范围内；若为空，则表示查询全部符合其他条件的记录", example = "2018-11-01 00:00:00", allowEmptyValue = true)
								 @RequestParam(value = "updateTime", required = false) String updateTime,
								 @ApiParam(value = "当前页数", defaultValue = "0")
								 @RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
								 @ApiParam(value = "每页大小，若该值为空或者为0，表示不使用分页，查询全部符合条件的记录；若值大于0，则对查询结果进行分页", defaultValue = "0")
								 @RequestParam(value = "limit", required = false, defaultValue = "0") Integer limit) {
		log.debug("==> \t进入查询单位的员工列表端点，传入参数：unitCode={};page={};limit={}", unitCode, page, limit);

		List<Staff> staffs = masterDataService.queryStaffs(unitCode, updateTime, page, limit);
		log.info("查询到{}条记录", staffs.size());
		ResponseEntity result = new ResponseEntity(staffs, HttpStatus.OK);
		return result;
	}

	@GetMapping("{unitCode}/{deptCode}/staffs")
	@ApiOperation(value = "查询单位部门的员工列表", notes = "查询单位部门的员工列表", response = Staff.class)
	public ResponseEntity staffs(@ApiParam(value = "单位代码", required = true)
								 @PathVariable("unitCode") String unitCode,
								 @ApiParam(value = "部门代码", required = true)
								 @PathVariable("deptCode") String deptCode,
								 @ApiParam(value = "数据更新时间，格式为：yyyy-MM-dd HH:mm:ss。该参数表示查询的数据记录更新时间在给定的时间到当前时刻范围内；若为空，则表示查询全部符合其他条件的记录", example = "2018-11-01 00:00:00", allowEmptyValue = true)
								 @RequestParam(value = "updateTime", required = false) String updateTime,
								 @ApiParam(value = "当前页数", defaultValue = "0")
								 @RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
								 @ApiParam(value = "每页大小，若该值为空或者为0，表示不使用分页，查询全部符合条件的记录；若值大于0，则对查询结果进行分页", defaultValue = "0")
								 @RequestParam(value = "limit", required = false, defaultValue = "0") Integer limit) {
		log.debug("==> \t进入查询单位部门的员工列表端点，传入参数：传入参数：unitCode={};deptCode={};page={};limit={}", unitCode, deptCode, page, limit);

		List<Staff> staffs = masterDataService.queryStaffs(unitCode, deptCode, updateTime, page, limit);
		log.info("查询到{}条记录", staffs.size());
		ResponseEntity result = new ResponseEntity(staffs, HttpStatus.OK);
		return result;
	}

	@GetMapping("staff/{code}")
	@ApiOperation(value = "查询员工的基本信息", notes = "查询员工的基本信息", response = Staff.class)
	public ResponseEntity staffs(@ApiParam(value = "员工代码", required = true)
								 @PathVariable("code") String code) {
		log.debug("==> \t进入查询员工的基本信息端点，传入参数：code={}", code);

		Staff staff = masterDataService.findStaff(code);
		log.info("查询到员工代码为{}的员工信息：{}", code, new Gson().toJson(staff));
		ResponseEntity result = new ResponseEntity(staff, HttpStatus.OK);
		return result;
	}
}
